<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1200" @ok="handleSubmit">
    <BasicForm @register="registerForm" ref="formRef" name="XlCustomerAftersaleForm" />
    <!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated>
      <a-tab-pane tab="商品列表" key="xlCustomerAftersaleDetail" :forceRender="true">
        <!--引用表格-->
        <BasicTable @register="registerTable" :rowSelection="rowSelection">
         
        </BasicTable>
      </a-tab-pane>
    </a-tabs>
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref, reactive } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema, subColumns } from '../XlCustomerAftersale.data';
import { detailList } from '../XlCustomerAftersale.api';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const formDisabled = ref(false);

//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 150,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 24 }
});


const dataId = ref('0')
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await reset();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  formDisabled.value = !data?.showFooter;
  if (unref(isUpdate)) {
    dataId.value = data.record?.id;  //主键id
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  }
  //reload();
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter })
});

//设置标题
const title = computed(() => (!unref(isUpdate) ? '新增' : !unref(formDisabled) ? '编辑' : '详情'));

//注册table数据
const { prefixCls, tableContext, } = useListPage({
  tableProps: {
    title: '商品列表',
    api: detailList,
    columns: subColumns,
    showActionColumn: false,
    canResize: false,
    showSummary: true,
    beforeFetch: (params) => {
      params.id = dataId.value
      return Object.assign(params);
    },
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext

async function reset() {
  await resetFields();
}

</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}
</style>